package davidlauzon.activerecord.visitor;

import davidlauzon.activerecord.nodes.Table;
import davidlauzon.activerecord.nodes.field.FloatField;
import davidlauzon.activerecord.nodes.field.IntegerField;
import davidlauzon.activerecord.nodes.field.StringField;

public class MysqlSerializer extends SqlSerializer
{

    @Override
    public String quote_column_name( String name ) {
        return "`" + name + "`";
    }
    
    @Override
    public String quote_table_name( String name ) {
        return "`" + name + "`";
    }
    
    @Override
    public String visitColumnDefinition( StringField field ) {
        log("visitColumnDefinition", "StringField");
        return quote_column_name(field.name()) + " VARCHAR(255) DEFAULT NULL";
    }

    @Override
    public String visitColumnDefinition( IntegerField field ) {
        log("visitColumnDefinition", "IntegerField");
        if (field.isPrimaryKey())
            return quote_column_name(field.name()) + " INT(11) NOT NULL AUTO_INCREMENT";
        else
            return quote_column_name(field.name()) + " INT(11) DEFAULT NULL";
    }

    @Override
    public String visitColumnDefinition( FloatField field ) {
        log("visitColumnDefinition", "FloatField");
        return quote_column_name(field.name()) + " FLOAT DEFAULT NULL";
    }
    
    @Override
    public String visitTableConstraints( Table table ) {
        log("visitTableConstraints", "Table");
        return ", PRIMARY KEY (" + quote_table_name(table.name()) + ") ";
    }
    
}
